<template>
  <!-- 注册表单 -->
  <el-form 
    ref="registerFormRef" 
    :model="registerForm" 
    :rules="registerRules" 
    label-position="top" 
    class="register-form"
  >
    <el-form-item label="用户名" prop="username">
      <el-input 
        v-model="registerForm.username" 
        placeholder="请输入用户名" 
        :prefix-icon="UserFilled"
      ></el-input>
    </el-form-item>
    
    <el-form-item label="邮箱" prop="email">
      <el-input 
        v-model="registerForm.email" 
        placeholder="请输入邮箱" 
        :prefix-icon="Message"
      ></el-input>
    </el-form-item>
    
    <el-form-item label="密码" prop="password">
      <el-input 
        v-model="registerForm.password" 
        type="password" 
        placeholder="请输入密码" 
        :prefix-icon="Lock"
        show-password
      ></el-input>
    </el-form-item>
    
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input 
        v-model="registerForm.confirmPassword" 
        type="password" 
        placeholder="请再次输入密码" 
        :prefix-icon="Lock"
        show-password
      ></el-input>
    </el-form-item>
    
    <el-form-item label="用户身份" prop="role">
      <el-select 
        v-model="registerForm.role" 
        placeholder="请选择用户身份" 
        class="role-select"
      >
        <el-option 
          v-for="role in registerRoleOptions" 
          :key="role.value" 
          :label="role.label" 
          :value="role.value"
        >
          <div class="role-option">
            <el-icon><component :is="role.icon" /></el-icon>
            <span>{{ role.label }}</span>
          </div>
        </el-option>
      </el-select>
    </el-form-item>
    
    <el-form-item class="register-btn-item">
      <el-button 
        type="primary" 
        class="register-button" 
        @click="handleRegister" 
        :loading="loading"
      >注册</el-button>
    </el-form-item>
    
    <!-- 添加底部留白区域 -->
    <div class="bottom-space"></div>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { UserFilled, Lock, Message, Van } from '@element-plus/icons-vue'
import { setUserRole, type UserRole } from '@/router'

const router = useRouter()
const loading = ref(false)

// 注册角色选项 - 只有客户和配送员可选
const registerRoleOptions = [
  { label: '客户', value: 'customer' as UserRole, icon: UserFilled },
  { label: '配送员', value: 'courier' as UserRole, icon: Van }
]

// 注册表单
const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  role: 'customer' as UserRole // 默认为客户角色
})

// 验证密码是否一致
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 注册表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择用户身份', trigger: 'change' }
  ]
}

// 注册表单引用
const registerFormRef = ref(null)

// 处理注册
const handleRegister = () => {
  registerFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true
      
      // 模拟注册请求
      setTimeout(() => {
        loading.value = false
        
        // 保存用户登录状态和角色
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('user_role', registerForm.role)
        localStorage.setItem('username', registerForm.username)
        
        setUserRole(registerForm.role)
        
        ElMessage.success('注册成功')
        
        // 根据角色导航到对应页面
        if (registerForm.role === 'customer') {
          router.push('/customer')
        } else if (registerForm.role === 'courier') {
          router.push('/courier')
        }
      }, 1000)
    } else {
      return false
    }
  })
}
</script>

<style scoped>
.register-form {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  padding-bottom: 6px;
}

:deep(.el-input__inner) {
  height: 42px;
  border-radius: 6px;
  border-color: #dcdfe6;
}

:deep(.el-input__inner:focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.role-select {
  width: 100%;
}

.role-option {
  display: flex;
  align-items: center;
}

.role-option .el-icon {
  margin-right: 8px;
}

.register-btn-item {
  margin-bottom: 0;
}

.register-button {
  width: 100%;
  margin-top: 10px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
  background: linear-gradient(90deg, #409eff 0%, #2d78c3 100%);
  border: none;
  box-shadow: 0 6px 12px rgba(64, 158, 255, 0.2);
  transition: all 0.3s ease;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(64, 158, 255, 0.3);
}

/* 底部留白区域 */
.bottom-space {
  height: 40px;
  width: 100%;
}
</style> 